package org.jeecg.modules.demo.com.kunpeng.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.com.kunpeng.entity.SupplierManagement;
import org.jeecg.modules.demo.com.kunpeng.entity.dto.SupplierManagementQueryListDTO;
import org.jeecg.modules.demo.com.kunpeng.ex.ServiceCode;
import org.jeecg.modules.demo.com.kunpeng.ex.ServiceException;
import org.jeecg.modules.demo.com.kunpeng.mapper.SupplierManagementMapper;
import org.jeecg.modules.demo.com.kunpeng.service.ISupplierManagementService;
import org.jeecg.modules.demo.com.kunpeng.vo.SupplierManagementQueryListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
 * @Description: 供应商管理表
 * @Author: jeecg-boot
 * @Date:   2023-01-09
 * @Version: V1.0
 */
@Service
@Slf4j
public class SupplierManagementServiceImpl extends ServiceImpl<SupplierManagementMapper, SupplierManagement> implements ISupplierManagementService {

    @Autowired
    private SupplierManagementMapper supplierManagementMapper;

    /**
     * 新增
     * @param supplierManagement
     */
    @Override
    public void insert(SupplierManagement supplierManagement) {
        List<SupplierManagement> supplierManagements = supplierManagementMapper.queryListBySupplierName(supplierManagement.getSupplierName());
        if(supplierManagements.size()>0){
            String message="该供货商已存在,请重新填写！";
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String userId = sysUser.getId();
        supplierManagement.setCreatorId(userId);
        supplierManagement.setIsDelete("0");
        supplierManagementMapper.insert(supplierManagement);
    }

    /**
     *  关键字查询
     * @param page
     * @param supplierManagementQueryListDTO
     * @return
     */
    @Override
    public Page<SupplierManagementQueryListVO> queryListByKeyWords(Page<SupplierManagementQueryListVO> page, SupplierManagementQueryListDTO supplierManagementQueryListDTO) {
        log.info("开始处理关键字查询：Service");
        Page<SupplierManagementQueryListVO> supplierManagementQueryListVOPage = page.setRecords(supplierManagementMapper.queryListByKeyWords(page, supplierManagementQueryListDTO.getClassification(), supplierManagementQueryListDTO.getKeyWords()));
        return supplierManagementQueryListVOPage;
    }

    /**
     * 修改是否删除状态
     * @param id
     * @param isDelete
     */
    @Override
    public void updateIsDelete(String id, String isDelete) {
        supplierManagementMapper.updateIsDelete(id, isDelete);
    }
}
